package fanjingzhu.check.dao.impl;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import publics.model.Application;
import fanjingzhu.check.dao.ApplicationDao;
@Component("applicationDao")
public class ApplicationDaoImpl implements ApplicationDao {
	
	private HibernateTemplate hibernateTemplate;// hibernate的spring代理模版

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	// 注入 hibernate模版
	@Resource(name = "hibernateTemplate")	
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
		System.out.println("hibernateTemplate injected in applicationDaoImpl!");
		// 测试用，实际运行中可去掉这句
	}
	
	public Integer save(Application application) {
		return (Integer)this.getHibernateTemplate().save(application);
	}

	public boolean delete(Application application) {
		try{
		this.getHibernateTemplate().delete(application);
		}catch(DataAccessException e){
		return false;
		}
		return true;
	}

	public boolean deleteById(Integer applicationId) {
		try{
		this.getHibernateTemplate().delete(this.getHibernateTemplate().get(Application.class, applicationId));
		}catch(DataAccessException e){
		return false;
		}
		return true;
	}
    
	public boolean update(Application application) {
		try{	
			this.getHibernateTemplate().update(application);
		}catch (DataAccessException e) {
			return false;
		}
		return true;
	}
	
	public int findByCount(String sql) {
		return this.getHibernateTemplate().find(sql).size();
	}

	public Application findById(Integer id) {
		return this.getHibernateTemplate().get(Application.class, id);
	}
	@SuppressWarnings("unchecked")
	public List<Application> findByPage(final String hql, final int offset, final int pageSize) {
		List<Application> list=this.getHibernateTemplate().executeFind(
				new HibernateCallback(){
					public Object doInHibernate(Session session) throws HibernateException ,SQLException {
						//执行hibernate分页查询
						List<Application> result = session.createQuery(hql)
							.setFirstResult(offset)
							.setMaxResults(pageSize)
							.list();
						return result;
					}
				}
			);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Application> findByQuery(String query) {
		return this.getHibernateTemplate().find(query);
	}

	/* (non-Javadoc)
	 * @see fanjingzhu.check.dao.ApplicationDao#findByEntId(java.lang.Integer)
	 */
	@SuppressWarnings("unchecked")
	public List<Application> findByEntId(Integer entId) {
		return this.getHibernateTemplate().find("from Application a where a.ent = ?  order by a.id desc ",entId);
	}

}	

	


